package com.songtech.login.service;

import com.songtech.login.model.SysPosUserExample;
import com.songtech.login.dao.SysPosUserMapper;
import com.songtech.login.model.SysPosUser;
import com.songtech.set.dao.TerminalInfoDao;
import com.songtech.set.modle.TerminalInfo;
import com.songtech.set.service.TermianlInfoService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

@Service
public class PLoginService {
    private Logger logger = Logger.getLogger(PLoginService.class);

    @Resource
    private SysPosUserMapper sysPosUserMapper;

    @Autowired
    private TermianlInfoService termianlInfoService;

    /**
     * 根据身份标识查询用户
     * @param imei
     * @return
     */
    public SysPosUser getUser(String imei){
        SysPosUser user = getUserByExample(imei);
        if(user == null){
            TerminalInfo terminalInfo = termianlInfoService.selectTerminalInfoByName(imei);
            if(terminalInfo != null && !StringUtils.isEmpty(terminalInfo.getImei())){
                user = getUserByExample(terminalInfo.getImei());
            }
        }
        return user;
    }

    /**
     * 根据imei查询用户
     * @param imei
     * @return
     */
    public SysPosUser getUserByExample(String imei){
        SysPosUserExample example = new SysPosUserExample();
        SysPosUserExample.Criteria criteria = example.createCriteria();
        criteria.andImeiEqualTo(imei);
        SysPosUser user = null;
        List<SysPosUser> sysPosUsers =sysPosUserMapper.selectByExample(example);
        if(CollectionUtils.isNotEmpty(sysPosUsers)){
            for (SysPosUser sysPosUser:sysPosUsers){
                user = sysPosUser;
            }
        }
        return user;
    }
    /**
     * 根据openid查询用户信息
     * @param openid
     * @return
     */
    public SysPosUser getUserByOpeniD(String openid) {
        SysPosUserExample example = new SysPosUserExample();
        SysPosUserExample.Criteria criteria = example.createCriteria();
        criteria.andOpenIdEqualTo(openid);
        SysPosUser user = null;
        List<SysPosUser> sysPosUsers =sysPosUserMapper.selectByExample(example);
        if(CollectionUtils.isNotEmpty(sysPosUsers)){
            for (SysPosUser sysPosUser:sysPosUsers){
                user = sysPosUser;
            }
        }
        return user;
    }

    public int updateUser(SysPosUser user){
        return sysPosUserMapper.updateByPrimaryKey(user);
    }

    public Boolean updatePwd(SysPosUser user){
        SysPosUserExample example = new SysPosUserExample();
        SysPosUserExample.Criteria criteria = example.createCriteria();
        criteria.andImeiEqualTo(user.getImei());
        List<SysPosUser> sysPosUsers =sysPosUserMapper.selectByExample(example);
        for (SysPosUser sysPosUser:sysPosUsers){
            sysPosUser.setPosUserPwd(user.getPosUserPwd());;
            int count = sysPosUserMapper.updateByPrimaryKey(sysPosUser);
            if(count != 1) {
                return false;
            }
        }
        return true;
    }

    public int deleteByOpenId(String openId){
        SysPosUser user =getUserByOpeniD(openId);
        int count = 0;
        if(user != null){
        	count = sysPosUserMapper.deleteByPrimaryKey(user.getPosUserId());
        }
        return count;
    }

    public List<SysPosUser> getAllUser(){
        SysPosUserExample example = new SysPosUserExample();
        SysPosUserExample.Criteria criteria = example.createCriteria();
        criteria.andOpenIdIsNotNull();
        List<SysPosUser> sysPosUsers =sysPosUserMapper.selectByExample(example);
        return sysPosUsers;
    }
}
